<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>吉大树洞 - 回复话题</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>

    <div class="background"></div>
    <div class="container">
        <div class="form-container">
            <h1>回复话题</h1>
            <form id="replyTopicForm">
                <label for="reply_content">回复内容:</label>
                <textarea id="reply_content" name="reply_content" rows="10" required></textarea>

                <button type="submit">回复</button>
            </form>
            <p><a href="topics.html">返回话题列表</a></p>
        </div>
    </div>

    <script>
        // 获取话题 ID
        const urlParams = new URLSearchParams(window.location.search);
        const topicId = urlParams.get('topicId');

        // 回复话题逻辑
        document.getElementById('replyTopicForm').addEventListener('submit', function (event) {
            event.preventDefault(); // 阻止表单默认提交行为

            // 获取回复内容
            const replyContent = document.getElementById('reply_content').value;

            // 创建回复对象
            const reply = {
                id: Date.now(), // 使用时间戳作为唯一 ID
                topicId,
                content: replyContent,
                timestamp: new Date().toLocaleString() // 添加时间戳
            };

            // 从 localStorage 获取回复列表
            let replies = JSON.parse(localStorage.getItem('replies')) || [];

            // 将新回复添加到回复列表
            replies.push(reply);

            // 将更新后的回复列表存储到 localStorage
            localStorage.setItem('replies', JSON.stringify(replies));

            alert('回复成功！');
            window.location.href = 'topics.html'; // 跳转到话题页面
        });
    </script>
</body>
</html>